<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();

$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$dp_code.'&sign='.$sign['sign'];

if (empty($this->Param['option'])) {
    
    //待初审
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        $where = array(
            '`o_status` IN(?)' => array('初审提交', '审核中'),
            '`o_trial_status` IN(?)' => array('审核中', '未审核'),
        );
        $where['`ot_type` = ?'] = '资料审核';

        if (empty($this->Param['o_keyword'])) {
            if ($member_info['u_role'] == 7) {//资料审核
                $where['`o_perch` IN (?)'] = array($member_info['u_id'], 0);
            }
        }else{
            $where['`o_trial_status` IN(?)'] = array('审核中', '未审核','暂停中');
        }

        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info, 'order_split', array("`o_layer_aucode` DESC", "FIELD(`o_layer_status`, '已通过','暂停中','审核中','未审核','未通过')", "`o_first_time` ASC"));
        $json_data = array('total' => $data['count'], 'rows' => array());
        
        $mode = Config::GetCommon();
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $row['o_search_time'] = date('Y-m-d H:i:s', $row['o_search_time']);
                $row['o_sell_time'] = date('Y-m-d H:i:s', $row['o_sell_time']);
                $row['o_first_time'] = date('Y-m-d H:i:s', $row['o_first_time']); //初审提交时间
                $order_car = $OrderHelper->orderCar($row['o_id']);
                $row['ot_loan_limit'] = $order_car['ot_loan_limit']; //贷款金额
                $row['o_pre_modetext'] = $mode['mode'][$row['o_pre_mode']]['mode_text'];
                $o_perch = $MemberHelper->getMemberRow($row['o_final_perch']);
                $row['o_final_perch_name'] = $o_perch['u_realname']; //占位人员
                $bank_row = $BankHelper->bankRow($row['o_bank_id']);
                $row['o_bank_abb'] = $bank_row['bk_abbreviation'];
                //
                if($row['o_oldsys'] == 1){
                    $sign = $oldapi->sign(array('code' => $dp_code));
                    $row['info_url'] = $api_baseurl.'&id='.$row['o_number'];
                }else{
                    $row['info_url'] = '/index/index/option/info?oid='.$row['o_id'];
                }
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    } else {        
        echo $this->GetView('order_ftolist.php');
    }
} else {
    switch($this->Param['option']){
        case 'troinfo':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');

            $upcount=$OrderHelper->orderuping($oid,'资审');
            if($upcount>0){
                echo "<script>alert('当前订单有未处理的修改申请');</script>";
            }

            //占位判断
            $result = $OrderHelper->perchJudge($oid, $member_info);
            if(!$result['success']){
                ErrorMsg::Debug($result['msg']);
            }
            
            $order = $OrderHelper->orderInfo($oid);
            $order['contractdetails'] = $OrderHelper->contractDetails($oid);            
            $order['audit_draft'] = $OrderHelper->orderAuditfilter($order['audit_all']['draft'], '初审审核');
            $this->Assign('order', $order);
            echo $this->GetView('order_ftoinfo.php');
            break;
        case 'save':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $save_type = empty($this->Param['savetype']) ? 'draft' : $this->Param['savetype'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            //file_put_contents('D:\work\chedai\www\wwwroot\dd13.txt',var_export($this->Param,true));
            $upcount=$OrderHelper->orderuping($oid,'资审');
            if($upcount>0){
                if($this->Param['a_results']=='可以办理') {
                    ErrorMsg::Debug('当前订单有未处理的修改申请，请处理完毕再来通过！');
                }
            }

            if($save_type == 'draft'){
                $this->Param['draft'] = true;
            }  else {
                $this->Param['draft'] = false;
            }
            $this->Param['a_uid'] = $this->UserConfig['Id'];
            $result = $OrderHelper->dataReview($this->Param, $oid);
            if($result['success']){
                if($this->Param['a_results']=='不予办理'&& $save_type=='save'){
                    $result1 = $OrderHelper->orderupUpdate(array(
                        'up_status' => '作废'
                    ),'',$oid);
                }
                if($save_type != 'draft'){
                    $MemberHelper->saveLog($this->UserConfig, '初审审核', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                    $suspendMsg = $OrderHelper->suspendMsg($oid);
                    if($this->Param['a_results']=='可以办理'){
                        $this->LoadModel('Public')->update('order',array('o_deposit'=>$this->Param['a_amount_paid'],'o_deposit_remarks'=>$this->Param['a_deposit_remarks']),array('`o_id` = ?' =>$oid));
                    }
                    if(!empty($suspendMsg))  $MessageHelper->sendSingle($suspendMsg);
                }else{
                    $this->LoadModel('Public')->update('order',array('o_deposit'=>$this->Param['a_amount_paid'],'o_deposit_remarks'=>$this->Param['a_deposit_remarks']),array('`o_id` = ?' =>$oid));
                }
                ErrorMsg::Debug($save_type == 'draft' ? '保存成功' : '审核成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'expinfo':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $this->LoadHelper('LogisticsHelper');
            $LogisticsHelper = new LogisticsHelper();
            $logistics = $LogisticsHelper->logisticsRow(array('`ls_oid` = ?' => $oid, '`ls_status` = ?' => 'ok'));
            $this->LoadResurces('aliyun/class.express');
            $express = new Express();
            $result = $express->getInfo($logistics['ls_express_no']);
            $this->Assign('data', $result['data']);
            echo $this->GetView('order_expinfo.php');
            break;
        case 'back': //订单退回
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $this->Assign('oid', $oid);
            echo $this->GetView('order_ftoback.php');
            break;
        case 'backsave': //退回保存
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $status = empty($this->Param['status']) ? ErrorMsg::Debug('请选择退回状态') : $this->Param['status'];
            $remarks = $this->Param['remarks'];
            $v_type = 'ftooprate';
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $result = $OrderHelper->orderBack($oid, $status, $remarks, array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']),$v_type,$this->Param['lock_car']);
            if($result['success']){

                $result1 = $OrderHelper->orderupUpdate(array(
                    'up_status' => '作废'
                ),'',$oid);

                $MemberHelper->saveLog($this->UserConfig, '初审退回', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                //发送消息
                $order_row = $OrderHelper->orderRow($oid);
                $MessageHelper->sendSingle(array(
                    'm_uid' => $order_row['o_uid'],
                    'm_title' => '初审退回',
                    'm_name' => $order_row['o_uname'],
                    'm_content' => $order_row['o_vip_name'].'初审退回',
                    'm_dateline' => NOW_TIME,
                    'm_oid' => $oid,
                    'm_info' => serialize(array('status' => '初审退回', 'code' => $OrderHelper->statusChange('退回')))
                ));
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'chkout': //订单签出
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $result = $OrderHelper->orderChkout($oid, '资料审核', array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']));
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '初审签出', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'suspend'://订单暂停
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $this->Assign('oid', $oid);
            echo $this->GetView('order_ftosuspend.php');
            break;
        case 'suspendsave'://暂停保存
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $remarks = $this->Param['remarks'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $result = $OrderHelper->orderSuspend($oid, $remarks, array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']), 'fto');
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '初审暂停', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                $suspendMsg = $OrderHelper->suspendMsg($oid);
                if(!empty($suspendMsg))  $MessageHelper->sendSingle($suspendMsg);
                ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'replace'://替换资审页面
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $member_arr = $OrderHelper->getOtherDataauditor($oid);
            $this->Assign('member_arr', $member_arr);
            $this->Assign('oid', $oid);
            echo $this->GetView('order_ftoreplace.php');
            break;
        case 'replacesave'://替换保存
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $o_perch = empty($this->Param['o_perch']) ? ErrorMsg::Debug('请选择审核人员') : $this->Param['o_perch'];
            $result = $OrderHelper->replaceDataAuditor($oid,$o_perch);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '替换资料审核员', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug($result['msg'], true);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        default :
    }
}